package 单周赛.history;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

/**
 *
 */
public class 第303场单周赛 {

    public static void main(String[] args) {

        int[][] gird = {{3, 1, 2, 2}, {1, 4, 4, 5}, {2, 4, 2, 2}, {2, 4, 2, 2}};
        System.out.println(equalPairs(gird));

        FoodRatings foodRatings = new FoodRatings(
                new String[]{"vhbibydfop", "ggtdlawxup", "lslcltacc", "bqgkfcy", "xxowyabq", "ezswv", "ewgvsuyvpo", "zgochpey", "epiaxxiv", "idpdiqlcp", "wqgrbvme", "wqxbxeovwe", "iduzk", "ahemklz", "ah", "rvuqnlydxu", "dldqmpheob", "lzeqwfb", "ykwjc", "dtrgnrcqr", "cwcgevzzz", "zejbynj", "ivhdsnjpho", "gsyewq", "ekqhtiijrj", "cthwbt", "gnbhjyhcil", "kkthw", "ktiqokzl", "zzroi", "cjwapjnzql", "hfum", "jrwby", "amhfyno", "bj", "fszije", "nbkpptir", "dgzmoxhhoe", "bnisfskgxi", "uepypxnsy", "afdz", "tdctkeg", "lfsohpzmk", "ewwqult", "iynyddno", "yyroyjzdg", "mcpea", "lzafjh", "jdvcxidtm", "qvkyqmupz", "kjop", "girgfkaib", "qecvnaxpsm", "itx", "xhjmq", "lwwbdxkz", "chthxuhex", "tghokrfica", "vrmjibxv", "oipnttbz", "yyfvlwvj", "erbtnw", "yezfiom", "ismarfqbuv", "vmfxlqbxwa", "pcwparohxf", "bpdrndbv", "owgjjitjfd", "fdwmxhli", "kffetdxvfx", "ruahmg", "aphbsbkimb", "nbkdfkgl", "rxehcqcrwu", "kxwlfxbe", "qukxd", "tewgnjuxnm", "jxfqwqe", "mdgwiok", "tbkmnx", "klzznopcn", "vjbcvddw", "yzhawtmpwp", "fhjichpi", "tapig", "glfbwolsoa", "xklqlzm"},
                new String[]{"kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh", "kggxajkarh"},
                new int[]{146, 562, 428, 619, 731, 483, 598, 619, 950, 446, 630, 556, 894, 911, 537, 359, 863, 995, 417, 702, 689, 667, 502, 691, 896, 582, 111, 319, 182, 953, 131, 372, 563, 963, 300, 42, 829, 187, 676, 509, 259, 688, 897, 614, 392, 170, 392, 1, 134, 79, 995, 371, 886, 430, 72, 830, 871, 758, 247, 333, 128, 987, 659, 933, 640, 198, 638, 585, 825, 904, 729, 214, 974, 935, 581, 985, 85, 987, 51, 174, 301, 635, 148, 492, 200, 275, 101});

        foodRatings.changeRating("nbkdfkgl", 908);
        foodRatings.changeRating("hfum", 256);
        System.out.println(foodRatings.highestRated("kggxajkarh"));

    }

    public char repeatedCharacter(String s) {
        boolean[] res = new boolean[26];
        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i) - 'a';
            if (res[index]) {
                return s.charAt(i);
            }
            res[index] = true;
        }
        return 'a';
    }

    /**
     * 哈希表
     *
     * @param grid
     * @return
     */
    public static int equalPairs(int[][] grid) {
        int count = 0;
        Map<String, Integer> rowMap = new HashMap<>();
        for (int i = 0; i < grid.length; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < grid[0].length; j++) {
                stringBuilder.append(grid[i][j])
                        .append(",");
            }
            String str = stringBuilder.toString();
            rowMap.put(str, rowMap.getOrDefault(str, 0) + 1);
        }

        Map<String, Integer> columnMap = new HashMap<>();
        for (int j = 0; j < grid[0].length; j++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < grid.length; i++) {
                stringBuilder.append(grid[i][j])
                        .append(",");
            }
            String str = stringBuilder.toString();
            columnMap.put(str, columnMap.getOrDefault(str, 0) + 1);
        }

        for (String str : rowMap.keySet()) {
            if (columnMap.containsKey(str)) {
                count += rowMap.get(str) * columnMap.get(str);
            }
        }
        return count;
    }

    /**
     * 哈希表+优先队列
     */
    static class FoodRatings {

        private Map<String, PriorityQueue<Food>> cuisineMap = new HashMap<>();

        private Map<String, Food> foodMap = new HashMap<>();

        public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
            for (int i = 0; i < foods.length; i++) {
                Food food = new Food();
                food.name = foods[i];
                food.rating = ratings[i];
                food.cuisine = cuisines[i];
                foodMap.put(food.name, food);

                PriorityQueue<Food> priorityQueue = cuisineMap.get(food.cuisine);
                if (priorityQueue == null) {
                    priorityQueue = new PriorityQueue<Food>(((o1, o2) -> {
                        // 大坑!!!，包装类比较值用 equals 方法
                        return o1.rating.equals(o2.rating) ? compare(o1.name, o2.name)
                                : o2.rating - o1.rating;
                    }));
                    cuisineMap.put(food.cuisine, priorityQueue);
                }
                priorityQueue.add(food);
            }
        }

        public void changeRating(String food, int newRating) {
            Food foodObj = foodMap.get(food);
            PriorityQueue<Food> priorityQueue = cuisineMap.get(foodObj.cuisine);
            priorityQueue.remove(foodObj);
            foodObj.rating = newRating;
            priorityQueue.add(foodObj);
        }

        public String highestRated(String cuisine) {
            Food food = cuisineMap.get(cuisine).peek();
            return food == null ? null : food.name;
        }

        class Food {

            private String name;

            private Integer rating;

            private String cuisine;

        }

        private int compare(String str1, String str2) {
            for (int i = 0; i < Math.min(str1.length(), str2.length()); i++) {
                if (str1.charAt(i) < str2.charAt(i)) {
                    return -1;
                } else if (str1.charAt(i) > str2.charAt(i)) {
                    return 1;
                }
            }
            return str1.length() - str2.length();
        }
    }

}
